package com.zhu.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhu.commons.utils.BeanCopyUtils;
import com.zhu.service.edu.ThreadLocalService.VideoDeleteThreadLocal;
import com.zhu.service.edu.feign.AliyunVodFeign;
import com.zhu.service.edu.mapper.EduVideoMapper;
import com.zhu.service.edu.pojo.EduChapter;
import com.zhu.service.edu.mapper.EduChapterMapper;
import com.zhu.service.edu.pojo.EduVideo;
import com.zhu.service.edu.pojo.vo.ChapterVo;
import com.zhu.service.edu.pojo.vo.VideoVo;
import com.zhu.service.edu.service.IEduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2022-07-04
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements IEduChapterService {
    @Autowired
    private EduChapterMapper chapterMapper;
    @Autowired
    private EduVideoMapper videoMapper;
    @Autowired
    private VideoDeleteThreadLocal videoDeleteThreadLocal;
    @Autowired
    private AliyunVodFeign aliyunVodFeign;
    /**
     * 根据id删除章节
     * @param id
     * @return
     */
    @Override
    public boolean deleteChapterById(String id) throws Exception {
        /**
         * 查出章节下的阿里云video并且删除视频
         */
         videoDeleteThreadLocal.deleteVideos(videoMapper,aliyunVodFeign,id );

        /**
         * 删除章节下的视频
         */
        LambdaQueryWrapper<EduVideo> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EduVideo::getChapterId,id);
        videoMapper.delete(queryWrapper);
        /**
         * 删除章节
         */
        int i = chapterMapper.deleteById(id);
        if(i==1){
            return true;
        }
        return false;
    }

    /**
     * 查询章节下的视频列表
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVo> nestedList(String courseId) {
        //获取章节列表  List<Chapter>
        //遍历 列表查找chapter_id 查询video列表  垃圾
        //方案二
        /**
         * 通过courseId获取List<Chapter>
         * 通过courseId 获取List<Video>
         * 获取章节信息列表
         * 获取视频信息列表
         * 组装List<ChapterVo>
         */
        LambdaQueryWrapper<EduChapter> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EduChapter::getCourseId,courseId);
        queryWrapper.orderByAsc(EduChapter::getSort);
        List<EduChapter> eduChapters = chapterMapper.selectList(queryWrapper);
        LambdaQueryWrapper<EduVideo> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(EduVideo::getCourseId,courseId);
        queryWrapper.orderByAsc(EduChapter::getSort);
        List<EduVideo> eduVideos = videoMapper.selectList(queryWrapper1);

        List<ChapterVo> chapterVoList=new ArrayList<>();

        for (EduChapter eduChapter : eduChapters) {
            ChapterVo chapterVo = BeanCopyUtils.copyBean(eduChapter, ChapterVo.class);
            chapterVoList.add(chapterVo);
            List<VideoVo> videoVoList=new ArrayList<>();
            for (EduVideo eduVideo : eduVideos) {
                if(eduVideo.getChapterId().equals(eduChapter.getId())){

                    VideoVo videoVo = BeanCopyUtils.copyBean(eduVideo, VideoVo.class);
                    videoVoList.add(videoVo);

//                    chapterVoList.add(chapterVo);
                }
            }
            chapterVo.setChildren(videoVoList);

        }

        return chapterVoList;
    }

    /**
     * 查找课程章节视频列表（方案二）
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVo> nestedList2(String courseId) {
       List<ChapterVo> chapterVoList= chapterMapper.nestedList2(courseId);
        return chapterVoList;
    }
}
